package Assignment1;

import java.util.*;

public class TestComparator {
    public static void main(String[] args) {
        Province p1 = new Province("广西","南宁市",19,14);
        Province p2 = new Province("广东","广州",1,21);
        Province p3 = new Province("四川","成都",6,18);
        Province p4 = new Province("山东","济南",3,16);
        List<Province> geometricObjectList = new ArrayList<Province>();
        geometricObjectList.add(p1);
        geometricObjectList.add(p2);
        geometricObjectList.add(p3);
        geometricObjectList.add(p4);

        // 以比较器为依据，升序，并输出.
        System.out.println("按照GDP排名排序");
        Collections.sort(geometricObjectList,new GDPComparator());
        forprint(geometricObjectList);    // 打印函数
        // 逆序排序列表元素，并输出
        System.out.println("逆序排序列表元素");
        Collections.reverse(geometricObjectList);
        forprint(geometricObjectList);    // 打印函数
        // 随机序列表元素，并输出。
        System.out.println("随机序列表元素：");
        Collections.shuffle(geometricObjectList,new Random(21) );
        forprint(geometricObjectList);    // 打印函数
        // 找最大值元素，并输出。
        System.out.println("地级市总数最大：");
        Province p = max(p1,p2,new NumberofitiesComparator());
        Province maxp2 = max(p,p3, new NumberofitiesComparator());
        Province maxp3 = max(maxp2,p4, new NumberofitiesComparator());
        maxp3.getprint();
    }
    public static Province max(Province p1,
                               Province p2, Comparator<Province> c) {
        if (c.compare(p1, p2) > 0)
            return p1;
        else
            return p2;
    }
    public static void forprint(List<Province> getlist){
        for (Province iter3:getlist
        ) {
            iter3.getprint();    // 调用Province的打印函数
        }
    }




}
